import {
  _decorator,
} from "cc";
import { EntityTypeEnum, IBullet } from "../../Common";
import { EntityManager } from "../../Base/EntityManager";
import { EntityStateEnum } from "../../Enum";
import { rad2Angle } from "../../Utils";
import { BulletStateMachine } from "./BulletStateMachine";
const { ccclass, property } = _decorator;

@ccclass("BulletManager")
export class BulletManager extends EntityManager {
  type: EntityTypeEnum
  init(data: IBullet) {
    this.type = data.type
    this.fsm = this.addComponent(BulletStateMachine);
    this.fsm.init(data.type);
    this.state = EntityStateEnum.Idle;
    // 子弹初始位置会闪烁，设置false就看不到了
    this.node.active = false // 初始化是false，下面渲染的时候改为true
  }
  render(data: IBullet) {
    this.node.active = true // 初始化是false，这里渲染的时候改为true
    const { direction, position } = data;
    this.node.setPosition(position.x, position.y);

    // 斜面 = 开方(方向的x的平方加上方向的y的平方)
    const side = Math.sqrt(direction.x ** 2 + direction.y ** 2)
    // 弧度 = 反三角函数(斜面分之对边)
    // const rad = Math.asin(direction.y / side)
    const angle = direction.x > 0 ? rad2Angle(Math.asin(direction.y / side)) : rad2Angle(Math.asin(-direction.y / side)) + 180
    this.node.setRotationFromEuler(0, 0, angle)
  }
}
